package zhai.docking.alipay.service.base.alipay.api;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayCommerceIotDeviceBindQueryRequest;
import com.alipay.api.request.AlipayCommerceIotDeviceBindRequest;
import com.alipay.api.request.AlipayCommerceIotDeviceUnbindRequest;
import com.alipay.api.response.AlipayCommerceIotDeviceBindQueryResponse;
import com.alipay.api.response.AlipayCommerceIotDeviceBindResponse;
import com.alipay.api.response.AlipayCommerceIotDeviceUnbindResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhai.docking.alipay.constant.AlipayConstant;
import zhai.docking.alipay.constant.LogConstant;
import zhai.docking.alipay.domain.base.alipay.*;
import zhai.docking.alipay.domain.base.alipay.common.BaseAlipayCommonOut;
import zhai.docking.alipay.domain.base.bizlog.BizLogDataTemp;
import zhai.docking.alipay.domain.base.bizlog.BizLogSave;
import zhai.docking.alipay.prop.AlipayApiProperties;
import zhai.docking.alipay.service.base.bizlog.BizLogSer;
import zhai.docking.alipay.util.JSONUtil;
import zhai.docking.alipay.util.ObjectUtil;
import zhai.docking.alipay.util.StringUtil;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.service.base.alipay.api
 * @project-name zhai-docking-alipay
 * @description 基础-支付宝-如意lite 服务实现
 * @create-datetime 2021/1/18 14:25
 */
@Slf4j
@Service
public class BaseAlipayApiRuyiIotSerImpl implements BaseAlipayApiRuyiIotSer {

    private static final String BUSINESS_NAME = "【基础-支付宝-如意lite 服务】";
    private static final String METHOD_QUERY = "（alipay.commerce.iot.device.bind.query(IoT设备-商户-店铺绑定查询)https://opendocs.alipay.com/pre-apis/01cmg5）";
    private static final String METHOD_BIND = "（alipay.commerce.iot.device.bind(IoT设备-商户-店铺绑定)https://opendocs.alipay.com/pre-apis/01cmg3）";
    private static final String METHOD_UNBIND = "（alipay.commerce.iot.device.unbind(IoT设备-商户-店铺解绑)https://opendocs.alipay.com/pre-apis/01cmg4）";

    @Autowired
    AlipayApiProperties alipayApiProp;
    @Autowired
    BizLogSer bizLogSer;

    @SneakyThrows
    @Override
    public BaseAlipayRuyiIotQueryOut query(BaseAlipayRuyiIotQueryIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_QUERY, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayCommerceIotDeviceBindQueryRequest request = new AlipayCommerceIotDeviceBindQueryRequest();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayCommerceIotDeviceBindQueryResponse response = alipayClient.execute(request);
        /** 操作：结果转换 */
        BaseAlipayRuyiIotQueryOut result = ObjectUtil.copy(response, BaseAlipayRuyiIotQueryOut.class);
        result.setBindInfoList(ObjectUtil.batchCopy(response.getBindInfoList(), BaseAlipayIotDeviceBindInfo.class));
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, request);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_QUERY, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }

    @SneakyThrows
    @Override
    public BaseAlipayRuyiIotBindOut bind(BaseAlipayRuyiIotBindIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_BIND, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayCommerceIotDeviceBindRequest request = new AlipayCommerceIotDeviceBindRequest();
        in.setSpiAppId(alipayApiProp.getAppid());
        request.setBizContent(JSONUtil.toStr(in));
        AlipayCommerceIotDeviceBindResponse response = alipayClient.execute(request);
        /** 操作：记录业务日志 */
        bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.IOT_RUYI_BIND_LOG));
        /** 操作：结果转换 */
        BaseAlipayRuyiIotBindOut result = ObjectUtil.copy(response, BaseAlipayRuyiIotBindOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_BIND, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }

    @SneakyThrows
    @Override
    public BaseAlipayRuyiIotUnbindOut unbind(BaseAlipayRuyiIotUnbindIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_UNBIND, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayCommerceIotDeviceUnbindRequest request = new AlipayCommerceIotDeviceUnbindRequest();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayCommerceIotDeviceUnbindResponse response = alipayClient.execute(request);
        /** 操作：记录业务日志 */
        bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.IOT_RUYI_UNBIND_LOG));
        /** 操作：结果转换 */
        BaseAlipayRuyiIotUnbindOut result = ObjectUtil.copy(response, BaseAlipayRuyiIotUnbindOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_UNBIND, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }
}